package cool.taomu.toolkit.test

import com.google.common.util.concurrent.FutureCallback
import com.google.common.util.concurrent.MoreExecutors
import cool.taomu.toolkit.executor.TaomuExecutors
import java.util.ArrayList
import java.util.List
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import org.junit.jupiter.api.Test
import org.slf4j.LoggerFactory
import com.google.common.util.concurrent.Futures
import java.util.concurrent.TimeoutException

class ExecutorUtilsTest {
    val static LOG = LoggerFactory.getLogger(TaomuExecutors)
    val static executor = MoreExecutors.listeningDecorator(
        Executors.newCachedThreadPool
    );
    val static sexecutor = MoreExecutors.listeningDecorator(
        Executors.newScheduledThreadPool(4)
    );

    @Test
    def void timeoutTest() {
        var e = new TaomuExecutors<String>(executor)
        e.tasks(#[
            [return "Hello Wrold"],
            [
                throw new Exception("Test")
            ],
            [
                throw new TimeoutException("TimeoutException")
            ]
        ]).allAsList
        .catchingAsync([
            if(it instanceof TimeoutException){
                return Futures.immediateFuture(#["超时默认值"])
            }
        ])
        /* .onErrorResume([
            if(it instanceof Exception){
                return Futures.immediateFuture(#["默认值"])
            }
        ])*/
        .onSuccess([println(it)])
        .log("test ")

    // Thread.sleep(Integer.MAX_VALUE);
    }
}
